﻿using JT808.Protocol.Extensions.JTActiveSafety.MessageBody;
using JT808.Protocol.Extensions.JTActiveSafety.Metadata;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace JTActiveSafety.Gateway
{
    //public class MemoryAttachFileRecvHandler : IAttachFileRecvService, IAttachFileRecvRecoredManage
    //{
    //    private readonly ILogger _logger;
    //    private readonly ConcurrentDictionary<string, uint> _attachFiles = new ConcurrentDictionary<string, uint>();
    //    private readonly ConcurrentDictionary<string, List<Node>> _receiveRecords = new ConcurrentDictionary<string, List<Node>>();

    //    public MemoryAttachFileRecvHandler(ILogger<MemoryAttachFileRecvHandler> logger)
    //    {
    //        _logger = logger;
    //    }

    //    public void CreateAlarmAttachFileInfo(string terminalPhoneNo, JT808_0x1210 t808_0X1210)
    //    {
    //        foreach (var item in t808_0X1210.AttachInfos)
    //        {
    //            _attachFiles.TryAdd(item.FileName, item.FileSize);
    //            _logger.LogWarning($"0x1210 FileName：{item.FileName} size-{item.FileSize}");
    //        }
    //    }

    //    public void AddFileReceiveRecord(string terminalPhoneNo, JT808_0x1299 jT808_0X1299)
    //    {
    //        if (_receiveRecords.TryRemove(jT808_0X1299.FileName, out var fileRecvItems))
    //        {
    //            fileRecvItems.Add(new Node() { Offset = jT808_0X1299.Offset, Length = jT808_0X1299.Length });
    //            _receiveRecords.TryAdd(jT808_0X1299.FileName, fileRecvItems);
    //            _logger.LogWarning($"0x1211_2 FileName：{jT808_0X1299.FileName} Offset-{jT808_0X1299.Offset} length-{jT808_0X1299.Length}");
    //        }
    //    }

    //    public CheckFileIntegrityO CheckFileIntegrity(string fileName, out uint fileSize, ref List<Node> items)
    //    {
    //        fileSize = _attachFiles.GetValueOrDefault(fileName);
    //        if (fileSize == default)
    //            return CheckFileIntegrityO.找不到对应文件记录;
    //        if (_receiveRecords.TryGetValue(fileName, out var fileRecvItems))
    //        {
    //            if (CheckLinkedListMissingNodes.TryCheck(fileRecvItems, fileSize, ref items))
    //            {
    //                return CheckFileIntegrityO.检查成功;
    //            }
    //            _logger.LogWarning($"Check File Integrity：{fileName} size-{fileSize}");
    //            return CheckFileIntegrityO.打不到文件数据包记录;
    //        }
    //        return CheckFileIntegrityO.数据有问题;
    //    }



    //    public bool ClearFileCache(string fileName)
    //    {

    //        _receiveRecords.TryRemove(fileName, out var _);
    //        _attachFiles.TryRemove(fileName, out var _);
    //        _logger.LogWarning($"Clear File Cache：{fileName}");
    //        return false;
    //    }
    //    public uint GetFileSize(string fileName)
    //    {
    //        return _attachFiles.GetValueOrDefault(fileName);
    //    }
    //}
}
